package com.taomeng.service.impl;

import com.taomeng.dao.ClassDao;
import com.taomeng.entity.Cls;
import com.taomeng.entity.ClsInfo;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.User;
import com.taomeng.service.ClassService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassDao classDao;


    @Override
    public List<Cls> findAll() {
        return classDao.findAll();
    }

    @Override
    public PageBean<ClsInfo> findByPage(int page, int limit, String key) {
        //获取当前操作的用户
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();

        if (subject.hasRole("admin")||subject.hasRole("headadmin")) {
            return classDao.findByPage(page, limit, key, null, null);
        } else if (subject.hasRole("fudaoyuan") || subject.hasRole("deptadmin")) {
            return classDao.findByPage(page, limit, key, principal.getDepartmentId(), null);
        } else if (subject.hasRole("banzhuren")) {
            return classDao.findByPage(page, limit, key, null, principal.getId());
        }
        return null;
    }

    @Override
    public void add(Cls cls) {
        classDao.add(cls);
    }

    @Override
    public void del(String id) {
        classDao.del(id);
    }

    @Override
    public boolean update(Cls cls) {
        return classDao.update(cls);
    }


}
